from database import SessionLocal
from models import TreeData
from sqlalchemy import or_

def check_incomplete_data():
    db = SessionLocal()
    
    try:
        # 查找数据不完整的记录
        incomplete_records = db.query(TreeData).filter(
            or_(
                TreeData.inspection_type.is_(None),
                TreeData.species.is_(None),
                TreeData.azimuth.is_(None),
                TreeData.horizontal_distance.is_(None),
                TreeData.plot_azimuth.is_(None),
                TreeData.plot_horizontal_distance.is_(None),
                TreeData.x.is_(None),
                TreeData.y.is_(None)
            )
        ).all()
        
        print(f"数据不完整的记录数: {len(incomplete_records)}")
        print("\n=== 数据不完整的记录详情 ===")
        
        for record in incomplete_records:
            missing_fields = []
            if record.inspection_type is None:
                missing_fields.append("检尺类型")
            if record.species is None:
                missing_fields.append("树种")
            if record.azimuth is None:
                missing_fields.append("方位角")
            if record.horizontal_distance is None:
                missing_fields.append("水平距离")
            if record.plot_azimuth is None:
                missing_fields.append("大样地方位角")
            if record.plot_horizontal_distance is None:
                missing_fields.append("大样地水平距离")
            if record.x is None:
                missing_fields.append("X坐标")
            if record.y is None:
                missing_fields.append("Y坐标")
            
            print(f"样木号: {record.sample_id}")
            print(f"  检尺类型: {record.inspection_type}")
            print(f"  树种: {record.species}")
            print(f"  2024胸径: {record.diameter_2024}")
            print(f"  2025胸径: {record.diameter_2025}")
            print(f"  方位角: {record.azimuth}")
            print(f"  水平距离: {record.horizontal_distance}")
            print(f"  大样地方位角: {record.plot_azimuth}")
            print(f"  大样地水平距离: {record.plot_horizontal_distance}")
            print(f"  坐标: ({record.x}, {record.y})")
            print(f"  备注: {record.note}")
            print(f"  缺少字段: {', '.join(missing_fields)}")
            print("-" * 50)
        
        # 统计各字段的空值数量
        print("\n=== 各字段空值统计 ===")
        fields = [
            ('inspection_type', '检尺类型'),
            ('species', '树种'),
            ('diameter_2024', '2024胸径'),
            ('diameter_2025', '2025胸径'),
            ('azimuth', '方位角'),
            ('horizontal_distance', '水平距离'),
            ('plot_azimuth', '大样地方位角'),
            ('plot_horizontal_distance', '大样地水平距离'),
            ('x', 'X坐标'),
            ('y', 'Y坐标'),
            ('note', '备注')
        ]
        
        for field_name, field_display in fields:
            null_count = db.query(TreeData).filter(getattr(TreeData, field_name).is_(None)).count()
            print(f"{field_display}: {null_count} 条空值")
        
    except Exception as e:
        print(f"检查过程中出现错误: {e}")
    finally:
        db.close()

if __name__ == "__main__":
    check_incomplete_data() 